package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.MedicalInstitution;
import com.lxw.nonghe.mapper.MedicalInstitutionMapper;
import com.lxw.nonghe.service.MedicalInstitutionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 医疗机构表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-04-17
 */
@Service
public class MedicalInstitutionServiceImpl extends ServiceImpl<MedicalInstitutionMapper, MedicalInstitution> implements MedicalInstitutionService {

    @Override
    public Page<MedicalInstitution> getMedicalInstitutionPage(int current, int size, String institutionName) {
        Page<MedicalInstitution> page = new Page<>(current,size);
        return (Page<MedicalInstitution>) this.baseMapper.selectMedicalInstitutionPage(page,institutionName);
    }

    @Override
    public MedicalInstitution getMedicalInstitutionByInstitutionName(String institutionName) {
        return this.lambdaQuery().eq(MedicalInstitution::getInstitutionName,institutionName).one();
    }

    @Override
    public MedicalInstitution getMedicalInstitutionByInstitutionCode(String institutionCode) {
        return this.lambdaQuery().eq(MedicalInstitution::getInstitutionCode,institutionCode).one();
    }

    @Override
    public Page<MedicalInstitution> queryMedicalInstitutions(MedicalInstitution queryParam, int current, int size) {
        // 参数校验
        if (current < 1) {
            current = 1;
        }
        if (size < 1 || size > 100) {
            size = 10;
        }

        // 初始化分页对象
        Page<MedicalInstitution> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<MedicalInstitution> wrapper = new LambdaQueryWrapper<>();

        // 精确查询条件
        wrapper.eq(queryParam.getInstitutionId() != null,
                        MedicalInstitution::getInstitutionId, queryParam.getInstitutionId())
                .eq(StringUtils.isNotBlank(queryParam.getRegionCode()),
                        MedicalInstitution::getRegionCode, queryParam.getRegionCode())
                .eq(queryParam.getInstitutionLevel() != null,
                        MedicalInstitution::getInstitutionLevel, queryParam.getInstitutionLevel());

        // 模糊查询条件
        wrapper.like(StringUtils.isNotBlank(queryParam.getInstitutionName()),
                        MedicalInstitution::getInstitutionName, queryParam.getInstitutionName())
                .likeRight(StringUtils.isNotBlank(queryParam.getInstitutionCode()),
                        MedicalInstitution::getInstitutionCode, queryParam.getInstitutionCode());

        // 添加排序
        wrapper.orderByDesc(MedicalInstitution::getInstitutionLevel)
                .orderByAsc(MedicalInstitution::getInstitutionId);

        return this.page(page, wrapper);
    }
    //获取医疗机构数量
    @Override
    public long getMedicalInstitutionCount(){
        return this.count();
    }
}
